package com.comprehensive.personalmovie.service.impl;

import com.comprehensive.common.dto.PageResponse;
import com.comprehensive.common.po.MovieEpisodes;
import com.comprehensive.common.po.MovieSeasons;
import com.comprehensive.common.po.Movies;
import com.comprehensive.personalmovie.feign.MovieFeignClient;
import com.comprehensive.personalmovie.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieFeignClient movieFeignClient;

    @Override
    public PageResponse<Movies> listPage(int pageNum, int pageSize,int type,String name) {
        return movieFeignClient.listPage(pageNum, pageSize,type,name);
    }

    @Override
    public Movies getMovieById(long id) {
        return movieFeignClient.getById(id);
    }

    @Override
    public boolean addMovie(Movies movie) {
        return movieFeignClient.save(movie);
    }

    @Override
    public boolean updateMovie(Movies movie) {
        return movieFeignClient.update(movie);
    }

    @Override
    public boolean deleteMovie(long id) {
        if (Objects.isNull(id)){
            return false;
        }
        Movies movie = movieFeignClient.getById(id);
        if (Objects.isNull(movie)){
            return false;
        }
        List<MovieSeasons> movieSeasons = movieFeignClient.getSeasonsByMovieId(movie.getId().longValue());
        if (!movieSeasons.isEmpty()){
            for (MovieSeasons movieSeason : movieSeasons) {
                List<MovieEpisodes> movieEpisodes = movieFeignClient.getEpisodesBySeasonsId(movieSeason.getId().longValue());
                if (!movieEpisodes.isEmpty()){
                    for (MovieEpisodes movieEpisode : movieEpisodes) {
                        movieFeignClient.deleteEpisodes(movieEpisode.getId().longValue());
                    }
                }
                movieFeignClient.deleteSeasons(movieSeason.getId().longValue());
            }
        }
        return movieFeignClient.delete(id);
    }

    @Override
    public List<MovieSeasons> getMovieSeasons(long movieId) {
        if (Objects.isNull(movieId)){
            return List.of();
        }
        return movieFeignClient.getSeasonsByMovieId(movieId);
    }

    @Override
    public boolean addMovieSeasons(MovieSeasons movieSeasons) {
        return movieFeignClient.saveSeasons(movieSeasons);
    }

    @Override
    public boolean updateMovieSeasons(MovieSeasons movieSeasons) {
        return movieFeignClient.updateSeasons(movieSeasons);
    }

    @Override
    public boolean deleteMovieSeasons(long id) {
        if (Objects.isNull(id)){
            return false;
        }
        List<MovieEpisodes> movieEpisodes = movieFeignClient.getEpisodesBySeasonsId(id);
        if (!movieEpisodes.isEmpty()){
            for (MovieEpisodes movieEpisode : movieEpisodes) {
                movieFeignClient.deleteEpisodes(movieEpisode.getId().longValue());  // 删除剧集
            }
        }
        return movieFeignClient.deleteSeasons(id);
    }
    @Override
    public MovieEpisodes getMovieEpisodesById(long id) {
        if (Objects.isNull(id)){
            return null;
        }
        return movieFeignClient.getEpisodesById(id);
    }
    @Override
    public List<MovieEpisodes> getMovieEpisodes(long seasonId) {
        if (Objects.isNull(seasonId)){
            return List.of();
        }
        return movieFeignClient.getEpisodesBySeasonsId(seasonId);
    }

    @Override
    public boolean addMovieEpisodes(MovieEpisodes movieEpisodes) {
        return movieFeignClient.saveEpisodes(movieEpisodes);
    }

    @Override
    public boolean updateMovieEpisodes(MovieEpisodes movieEpisodes) {
        return movieFeignClient.updateEpisodes(movieEpisodes);
    }

    @Override
    public boolean deleteMovieEpisodes(long id) {
        return movieFeignClient.deleteEpisodes(id);
    }
}
